﻿/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 18/01/2011
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoLogo
    {
        public daoLogo()
        {

        }
        public LogoCollection List(Logo Logo)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            LogoCollection items = new LogoCollection();
            try
            {
                para[0] = new SqlParameter(busLogo.OrderBy, SqlDbType.NVarChar);
                para[0].Value = Logo.OrderBy;
                para[1] = new SqlParameter(busLogo.OrderDirection, SqlDbType.NVarChar);
                para[1].Value = Logo.OrderDirection;
                para[2] = new SqlParameter(busLogo.CurrentPage, SqlDbType.Int);
                para[2].Value = Logo.CurrentPage;
                para[3] = new SqlParameter(busLogo.PageSize, SqlDbType.Int);
                para[3].Value = Logo.PageSize;
                para[4] = new SqlParameter(busLogo.Condition, SqlDbType.NVarChar);
                para[4].Value = Logo.Condition;
                para[5] = new SqlParameter(busLogo.TotalRecords, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Logo_List", para);
                while (de.Read())
                {
                    Logo item = Load(de);
                    items.Add(item);
                }

                de.Close();
                Logo.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoLogo", "List(Logo Logo)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }

        public Logo GetByID(int logoId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Logo item = new Logo();
            try
            {
                para[0] = new SqlParameter(busLogo.LogoId, SqlDbType.Int);
                para[0].Value = logoId;
                using (de = clsSQL.SPDataReader("Logo_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }
                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoLogo", "GetByID(int logoId)", ex.ToString());
            }
            finally
            {
                de = null;

                clsSQL.Dispose();
                clsSQL = null;
            }
            return item;
        }
        private Logo Load(IDataReader de)
        {
            Logo item = new Logo();
            //item.LogoID = clsConvert.Int(clsString.CheckObject(de["LogoID"]));
            item.LogoId = clsConvert.DbToInt("LogoId", de);
            item.Title = clsConvert.DbToString("Title", de);
            item.TitleEn = clsConvert.DbToString("TitleEn", de);
            item.Link = clsConvert.DbToString("Link", de);
            item.ImageFile = clsConvert.DbToString("ImageFile", de);
            item.Target = clsConvert.DbToString("Target", de);
            item.Position = clsConvert.DbToByte("Position", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }

        /// <summary>
        /// Move item up
        /// </summary>
        /// <param name="LogoID"></param>
        /// <returns></returns>
        public bool OrderUp(int LogoID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLogo.LogoId, SqlDbType.Int);
                para[0].Value = LogoID;
                result = clsSQL.SPExecute("Logo_OrderUp", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLogo", "Logo_OrderUp(int LogoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        /// <summary>
        /// Move item down
        /// </summary>
        /// <param name="LogoID"></param>
        /// <returns></returns>
        public bool OrderDown(int LogoID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLogo.LogoId, SqlDbType.Int);
                para[0].Value = LogoID;
                result = clsSQL.SPExecute("Logo_OrderDown", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLogo", "OrderDown(int LogoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int LogoID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLogo.LogoId, SqlDbType.Int);
                para[0].Value = LogoID;
                result = clsSQL.SPExecute("Logo_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLogo", "Active(int LogoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool DeActive(int LogoID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLogo.LogoId, SqlDbType.Int);
                para[0].Value = LogoID;
                result = clsSQL.SPExecute("Logo_DeActive", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLogo", "DeActive(int LogoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int LogoID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busLogo.LogoId, SqlDbType.Int);
                para[0].Value = LogoID;
                result = clsSQL.SPExecute("Logo_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLogo", "Delete(int LogoID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Insert(Logo objLogo)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[11];
            try
            {
                para[0] = new SqlParameter(busLogo.Title, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objLogo.Title);
                para[1] = new SqlParameter(busLogo.Link, SqlDbType.VarChar);
                para[1].Value = clsConvert.StringToDB(objLogo.Link);
                para[2] = new SqlParameter(busLogo.ImageFile, SqlDbType.VarChar);
                para[2].Value = clsConvert.StringToDB(objLogo.ImageFile);
                para[3] = new SqlParameter(busLogo.Target, SqlDbType.VarChar);
                para[3].Value = clsConvert.StringToDB(objLogo.Target);
                para[4] = new SqlParameter(busLogo.IsActive, SqlDbType.Bit);
                para[4].Value = objLogo.IsActive;
                para[5] = new SqlParameter(busLogo.Position, SqlDbType.TinyInt);
                para[5].Value = objLogo.Position;
                para[6] = new SqlParameter(busLogo.CreatedBy, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objLogo.CreatedBy);
                para[7] = new SqlParameter(busLogo.CreatedDate, SqlDbType.DateTime);
                para[7].Value = objLogo.CreatedDate;
                para[8] = new SqlParameter(busLogo.UpdatedBy, SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objLogo.UpdatedBy);
                para[9] = new SqlParameter(busLogo.UpdatedDate, SqlDbType.DateTime);
                para[9].Value = objLogo.UpdatedDate;
                para[10] = new SqlParameter(busLogo.TitleEn, SqlDbType.NVarChar);
                para[10].Value = clsConvert.StringToDB(objLogo.TitleEn);
                result = clsSQL.SPExecute("Logo_Insert", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLogo", "Logo objLogo", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool Update(Logo objLogo)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[10];
            try
            {
                para[0] = new SqlParameter(busLogo.Title, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objLogo.Title);
                para[1] = new SqlParameter(busLogo.Link, SqlDbType.VarChar);
                para[1].Value = clsConvert.StringToDB(objLogo.Link);
                para[2] = new SqlParameter(busLogo.ImageFile, SqlDbType.VarChar);
                para[2].Value = clsConvert.StringToDB(objLogo.ImageFile);
                para[3] = new SqlParameter(busLogo.Target, SqlDbType.VarChar);
                para[3].Value = clsConvert.StringToDB(objLogo.Target);
                para[4] = new SqlParameter(busLogo.IsActive, SqlDbType.Bit);
                para[4].Value = objLogo.IsActive;
                para[5] = new SqlParameter(busLogo.Position, SqlDbType.TinyInt);
                para[5].Value = objLogo.Position;
                para[6] = new SqlParameter(busLogo.UpdatedBy, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objLogo.UpdatedBy);
                para[7] = new SqlParameter(busLogo.UpdatedDate, SqlDbType.DateTime);
                para[7].Value = objLogo.UpdatedDate;
                para[8] = new SqlParameter(busLogo.LogoId, SqlDbType.Int);
                para[8].Value = objLogo.LogoId;
                para[9] = new SqlParameter(busLogo.TitleEn, SqlDbType.NVarChar);
                para[9].Value = clsConvert.StringToDB(objLogo.TitleEn);
                result = clsSQL.SPExecute("Logo_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoLogo", "Update(Logo objLogo)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public LogoCollection GetByPosition(byte position)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            LogoCollection items = new LogoCollection();
            try
            {
                para[0] = new SqlParameter(busLogo.Position, SqlDbType.TinyInt);
                para[0].Value = position;
                de = clsSQL.SPDataReader("Logo_GetByPosition", para);
                while (de.Read())
                {
                    Logo item = Load(de);
                    items.Add(item);
                }
                de.Close();               
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoLogo", "GetByPosition(byte position)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
    }
}


